package com.tgy.core.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgy.core.mapper.BlogCategoryMapper;
import com.tgy.core.service.BlogCategoryService;
import com.tgy.page.PageQuery;
import com.tgy.page.TableDataInfo;
import com.tgy.pojo.BlogCategory;
import com.tgy.result.LayuiTableResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 文章分类信息Service业务层处理
 *
 * @author 自动生成
 * @date 2022-06-20
 */
@Service
public class BlogCategoryServiceImpl extends ServiceImpl<BlogCategoryMapper, BlogCategory> implements BlogCategoryService {

    @Resource
    private BlogCategoryMapper baseMapper;

    /**
     * 查询文章分类信息
     *
     * @param id 文章分类信息主键
     * @return 文章分类信息
     */
    @Override
    public BlogCategory queryById(String id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询文章分类信息列表
     *
     * @param bo 文章分类信息
     * @return 文章分类信息
     */
    @Override
    public TableDataInfo<BlogCategory> queryPageList(BlogCategory bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogCategory> lqw = buildQueryWrapper(bo);
        Page<BlogCategory> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public LayuiTableResult queryLayuiPageList(BlogCategory bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogCategory> lqw = buildQueryWrapper(bo);
        Page<BlogCategory> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return new LayuiTableResult(result.getTotal(),result.getRecords());
    }

    /**
     * 查询文章分类信息列表
     *
     * @param bo 文章分类信息
     * @return 文章分类信息
     */
    @Override
    public List<BlogCategory> queryList(BlogCategory bo) {
        LambdaQueryWrapper<BlogCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<BlogCategory> buildQueryWrapper(BlogCategory bo) {
        LambdaQueryWrapper<BlogCategory> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()),BlogCategory::getName,bo.getName());
        lqw.orderByAsc(BlogCategory::getSort);
        return lqw;
    }

    /**
     * 新增文章分类信息
     *
     * @param bo 文章分类信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(BlogCategory bo) {
        // validEntityBeforeSave(BlogCategory);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            bo.setId(bo.getId());
        }
        return flag;
    }

    /**
     * 修改文章分类信息
     *
     * @param bo 文章分类信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(BlogCategory bo) {
        // PBizPatient update = BeanUtil.toBean(bo, PBizPatient.class);
        // validEntityBeforeSave(BlogCategory);
        return baseMapper.updateById(bo) > 0;
    }

    @Override
    public Boolean deleteById(String id) {
        return baseMapper.deleteById(id) > 0;
    }


    /**
     * 批量删除文章分类信息
     *
     * @param ids 需要删除的文章分类信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        /*if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }*/
        return baseMapper.deleteBatchIds(ids) > 0;
    }


}
